package com.example.demo.mybatis.demo;

import com.example.demo.mybatis.mapper.CoffeeMapper;
import com.example.demo.mybatis.model.Coffee;
import com.example.demo.mybatis.model.CoffeeExample;
import com.example.demo.mybatis.model.CoffeeOrder;
import com.example.demo.mybatis.model.OrderState;
import com.example.demo.mybatis.service.CoffeeService;
import com.example.demo.mybatis.service.OrderService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author: YsCy丶
 * @Date: 2020/8/1 18:47
 * @descrotion PageHelper示例
 */
@Slf4j
public class PageHelper {

    @Autowired
    private CoffeeMapper coffeeMapper;
    @Autowired
    private CoffeeService coffeeService;
    @Autowired
    private OrderService orderService;

    private void playWithPageHelper(){

        coffeeMapper.findAllWithRowBounds(new RowBounds(1,3))
                .forEach(coffee -> log.info("Page(1) Coffee{}",coffee));
        coffeeMapper.findAllWithRowBounds(new RowBounds(2,3))
                .forEach(coffee -> log.info("Page(2) Coffee{}",coffee));

        log.info("分割线{}","============");

        coffeeMapper.findAllWithRowBounds(new RowBounds(1,0))
                .forEach(coffee -> log.info("Page(1) Coffee{}",coffee));

        log.info("================");

        coffeeMapper.findAllWithParam(1,3)
                .forEach(coffee -> log.info("Page(1) Coffee{}",coffee));
        List<Coffee> list = coffeeMapper.findAllWithParam(2,3);

        PageInfo<Coffee> info = new PageInfo<>(list);
        log.info("PageInfo:{}",info);
    }
    private void playWithMyBatis(){
        log.info("All Coffee:{}",coffeeMapper.selectByExample(new CoffeeExample()));
        List<Coffee>coffeeList =  coffeeService.findOneCoffeeByName("Latte");
        if(null != coffeeList){
            CoffeeOrder order =
                    orderService.createOrder("Li Lei",coffeeList.get(0),coffeeList.get(1));
            log.info("Update INIT to PAID: {}", orderService.updateState(order, OrderState.PAID));
            log.info("Update PAID to INIT: {}", orderService.updateState(order, OrderState.INIT));
        }
    }
}
